<?php
/*
 * 编辑问题
 */
defined('IN_TS') or die('Access Denied.');
$askid = intval($_GET['askid']);

// 用户是否登录
$userid = aac('user') -> isLogin();
//判断问题所有者
$askarr = $new['ask']->find('ask_topic', array('askid'=>$askid));
if (!$askarr)
{
	tsNotice('该问题不存在或已被删除');
}
if ($askarr['userid'] != $userid)
{
	tsNotice('无权编辑该问题');
}

$askarr['content'] = tsDecode($askarr['content']);

//编辑提交
if ($_POST)
{
	$title = trim($_POST['title']);
	$content = tsCleanContent($_POST['content']);
	//form值过滤 判断
	if ($title == '')
	{
		tsNotice('不要这么偷懒嘛，多少请写一点内容哦^_^');
	}
	if (mb_strlen($title, 'utf8') > 64)
	{
		// 限制发表内容多长度，默认为64
		tsNotice('标题很长很长很长很长...^_^');
	}
	if (mb_strlen($content, 'utf8') > 5000)
	{
		// 限制发表内容多长度，默认为5000
		tsNotice('发这么多内容干啥^_^');
	}
	
	$groupid = 0;
	if (isset($_POST['isSync']) && isset($_POST['group']))
	{
		$groupid = intval($_POST['group']);
	}
	
	//查找该组是否已存在该问题
	$strAsk = $new['ask']->find('ask_topic', array('askid'=>$askid));
	$now = time();
	
	if (0 != $groupid)
	{
		//之前没有同步的 插入新记录
		if (0 != $strAsk['groupid'])
		{
			//有同步到小组的动作 更新
			$new['ask']->update('group_topics', array(
					'typeid'=>0,
					'userid'=>$userid,
					'askid'=>$askid
			),array(
					'title'=>$title,
					'groupid'=>$groupid,
					'uptime'=>$now
			));
		}
		else
		{
			$new['ask']->create('group_topics', array(
					'typeid'=>0,
					'userid'=>$userid,
					'groupid'=>$groupid,
					'askid'=>$askid,
					'title'=>$title,
					'addtime'=>$now,
					'uptime'=>$now
			));
		}
	}
	else
	{
		if (0 != $strAsk['groupid'])
		{
			//删除之前小组同步的问题
			$new['ask']->delete('group_topics', array(
					'typeid'=>0,
					'userid'=>$userid,
					'groupid'=>$strAsk['groupid'],
					'askid'=>$askid
			));
		}
	}
	
	//问答表
	$new['ask']->update('ask_topic', array('askid'=>$askid), array(
			'title'=>$title,
			'content'=>$content,
			'groupid'=>$groupid
	));
	
	//删除先前的分类
	$questionCate = $new['ask']->findAll('ask_topic_cate', array('askid'=>$askid));
	foreach ($questionCate as $value)
	{
		//更改分类数
		$new['ask']->reduce_cate_one($value['cateid']);
	}
	$new['ask']->delete('ask_topic_cate', array('askid'=>$askid));
	
	//分类处理
	if (isset($_POST['tag']))
	{
		foreach ($_POST['tag'] as $val)
		{
			$cateid = intval($val);
			//插入问答分类对应表
			$new['ask']->create('ask_topic_cate', array('askid'=>$askid, 'cateid'=>$cateid));
			
			//更改分类数
			$new['ask']->add_cate_one($cateid);
		}
	}
	
	/* if ($groupid != $strAsk['groupid'])
	{
		//删除之前小组同步的问题
		$new['ask']->delete('group_topics', array(
				    'typeid'=>0,
					'userid'=>$userid,
					'groupid'=>$strAsk['groupid'],
					'askid'=>$askid
				));
		
		//有选择同步到小组
		if (0 != $groupid)
		{
			//插入新同步的小组问题
			$new['ask']->create('group_topics', array(
					'typeid'=>0,
					'userid'=>$userid,
					'groupid'=>$groupid,
					'askid'=>$askid,
					'title'=>$title,
					'addtime'=>$now,
					'uptime'=>$now
			));
		}
	}
	else
	{
		if (0 != $groupid)
		{
			//还是先前同步的小组
			$new['ask']->update('group_topics', array(
					'typeid'=>0,
					'userid'=>$userid,
					'groupid'=>$groupid,
					'askid'=>$askid
					),array(
					'title'=>$title,
				    'uptime'=>$now		
				    ));
		}
	} */
	
	header('Location:'.tsUrl('ask','question',array('id'=>$askid)));
	exit();
}

$title = '编辑问题';
//取出分类
$arrCate = $new['ask']->findAll('ask_cate');

//取出用户所加入的小组
$arrGroups = $new['ask']->findAll('group_users', array('userid'=>$userid));
$grouparr = array();
if ($arrGroups)
{
	$arrGroupsid = array();
	foreach($arrGroups as $val)
	{
		$arrGroupsid[] = $val['groupid'];
	}

	$arrGroupsid = implode(',', $arrGroupsid);
	if ('' != $arrGroupsid)
	{
		$sql = "select groupid,groupname from ".dbprefix."group where groupid in (".$arrGroupsid.")";
		$grouparr = $new['ask']->db->fetch_all_assoc($sql);
	}
}
//取出该问题分类
$questionCate = $new['ask']->findAll('ask_topic_cate', array('askid'=>$askid));
include template('edit');